package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Add implements IOperation {
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        int current = bookList.getUsed();

        //判断书架是否已存在此书
        for (int i = 0; i < current; i++) {
            Book book1 = bookList.getBooks(i);
            if (book1.getName().equals(name)) {
                System.out.println("书架已存在此书，请勿重复存放！");
                return;
            }
        }

        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        double price = scanner.nextDouble();
        scanner.nextLine();
        System.out.println("请输入类型：");
        String type = scanner.nextLine();

        Book book = new Book(name, author, price, type);

        bookList.setBooks(current, book);
        bookList.setUsed(current+1);
        System.out.println("新增成功");
    }
}
